package com.kxf.androidtestdemo.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Environment;
import android.util.Log;

import com.google.gson.ExclusionStrategy;
import com.google.gson.FieldAttributes;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.kxf.androidtestdemo.R;
import com.kxf.androidtestdemo.bean.TestBean;
import com.kxf.androidtestdemo.bean.User;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.util.Map;

public class GsonTestActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gson_test);

        TestBean tb = new TestBean();
        TestBean.Child[] chs = new TestBean.Child[3];
        for (int i = 0; i < 3; i++) {
            TestBean.Child ch = new TestBean.Child();
            ch.setNameCh("bbb" + i);
            ch.setInfoCh("dddinfoCh" + i);
            chs[i] = ch;
        }

        tb.setChs(chs);
        tb.setAge(101);
        tb.setFlag(true);
        tb.setName("aaa");
        tb.setInfo("info");
        Gson gson = new Gson();
        String str = gson.toJson(tb);
        Log.i("GsonTestActivity", "str = " + str);

        Gson gson1 = new GsonBuilder().disableInnerClassSerialization().create();
        String str1 = gson1.toJson(tb);
        Log.i("GsonTestActivity", "str1 = " + str1);

        Gson gson2 = new GsonBuilder().disableInnerClassSerialization().create();
        TestBean tb2 = gson2.fromJson(str, TestBean.class);
        Log.i("GsonTestActivity", "tb2 = " + tb2);

        Gson gson3 = new GsonBuilder()
                .addDeserializationExclusionStrategy(new ExclusionStrategy() {//反序列化
                    @Override
                    public boolean shouldSkipField(FieldAttributes f) {
                        Log.i("GsonTestActivity", "f.getName() = " + f.getName());
                        if ("info".equals(f.getName())){
                            Log.i("GsonTestActivity", "return true;  " + f.getName());
                            return true;
                        }
                        return false;
                    }

                    @Override
                    public boolean shouldSkipClass(Class<?> clazz) {
                        Log.i("GsonTestActivity", "clazz.getName() = " + clazz.getName());
                        if (Integer.class == clazz || int.class == clazz){
                            Log.i("GsonTestActivity", "return true;  " + clazz.getName());
                            return true;
                        }
                        return false;
                    }
                })
                .addSerializationExclusionStrategy(new ExclusionStrategy() {
                    @Override
                    public boolean shouldSkipField(FieldAttributes f) {
                        Log.i("GsonTestActivity", "addSerializationExclusionStrategy f.getName() = " + f.getName());
                        if ("name".equals(f.getName())){
                            Log.i("GsonTestActivity", "addSerializationExclusionStrategy return true;  " + f.getName());
                            return true;
                        }
                        return false;
                    }

                    @Override
                    public boolean shouldSkipClass(Class<?> clazz) {
                        Log.i("GsonTestActivity", "addSerializationExclusionStrategy clazz.getName() = " + clazz.getName());
                        if (Boolean.class == clazz || boolean.class == clazz){
                            Log.i("GsonTestActivity", "addSerializationExclusionStrategy return true;  " + clazz.getName());
                            return true;
                        }
                        return false;
                    }
                }).create();

        String str3 = gson3.toJson(tb);
        Log.i("GsonTestActivity", "str3 = " + str3);
        TestBean tb3 = gson3.fromJson(str, TestBean.class);
        Log.i("GsonTestActivity", "tb3 = " + tb3);

        Map map = new HashMap();
        TestBean.Child ch1 = new TestBean.Child();
        ch1.setInfoCh("88");
        ch1.setNameCh("888");
        map.put("1", "1111");
        map.put(ch1, "8888");
        String mapstr = gson.toJson(map);
        Log.i("GsonTestActivity", "map = " + map);
        Log.i("GsonTestActivity", "mapstr = " + mapstr);
        Map map1 = gson.fromJson(mapstr, Map.class);
        Log.i("GsonTestActivity", "map1 = " + map1);
        Map map2 = gson.fromJson(mapstr, new TypeToken<Map>() {}.getType());//new TypeToken<LIST>() {}.getType()
        Log.i("GsonTestActivity", "map2 = " + map2);

        try {
            File file = new File(Environment.getExternalStorageDirectory(), "ObjectOut.txt");
            if (false){
                ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream(file));
                User u = new User();
                u.setName("hahahahhah");
                u.setAge(190);
                os.writeObject(u);
                os.flush();
                os.close();
            }else {
                ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream(file));
                User u = (User) inputStream.readObject();
                inputStream.close();
                Log.i("GsonTestActivity", "ObjectInputStream  u = " + u);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
